<template>
  <div class="login-contain">
    <van-nav-bar title="登录" class="my-nav-bar" />
    <van-form @submit="onSubmit">
      <van-field
        v-model="mobile"
        name="mobile"
        label="手机号"
        placeholder="手机号"
        required
        :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern: /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请输入正确验证码'
          }
        ]"
      />
      <van-field
        v-model="code"
        name="code"
        label="验证码"
        placeholder="验证码"
        required
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /^\d{6}$/, message: '请输入正确验证码' }
        ]"
      />
      <div style="margin: 16px;">
        <van-button
          round
          block
          type="info"
          :loading="isLoading"
          :disabled="isLoading"
          native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { userLogin } from '../../api/login.js'
import { setToken } from '../../utils/token.js'
export default {
  data () {
    return {
      mobile: '13912345678',
      code: '246810',
      isLoading: false
    }
  },
  methods: {
    onSubmit (values) {
      // console.log('submit', values)
      this.isLoading = true
      userLogin(values)
        .then(res => {
          console.log(res)
          this.$toast.success('登录成功')
          setToken(res.data.data)
          setTimeout(() => {
            this.isLoading = false
            this.$router.push({ path: '/home' })
          }, 400)
        })
        .catch(err => {
          console.log(err)
          this.$toast.fail('登录失败')
          setTimeout(() => {
            this.isLoading = false
          }, 400)
        })
    }
  }
}
</script>

<style lang="less">
.login-contain {
  .my-nav-bar {
    background-color: #3196fa;
    .van-nav-bar__title {
      color: #fff;
    }
  }
}
</style>
